//
//  HVWBorderShownCardsView.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/6.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "HVWBorderShownCardsView.h"
#import "HVWBorderShownCardCell.h"
#import "ActivityDetailController.h"
#import "HomeList.h"
#import "ScanIDCardFontViewController.h"

NSString *const CellIdentifier = @"CellIdentifier";


@interface HVWBorderShownCardsView() <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) UIScrollView *panScrollView;

@property(nonatomic, assign, getter=isMultiplePages) BOOL multiplePage;
@property(nonatomic, strong) NSTimer *timer;

@end

@implementation HVWBorderShownCardsView

- (instancetype)initWithFrame:(CGRect)frame With:(NSInteger )type
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViewWith:type];
    }
    return self;
}
- (void)setupViewWith:(NSInteger)type{
    CGFloat collectionViewWidth = self.frame.size.width;
    CGFloat collectionViewHeight = self.frame.size.height;
    
    CGFloat itemWidth = collectionViewWidth;
    if (type == 1) {
        itemWidth = collectionViewWidth - 15 * 2;
    }else{
        itemWidth = collectionViewWidth - 33 * 2;
    }
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(itemWidth, collectionViewHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    if (type == 1) {
        layout.minimumLineSpacing = 7;
        layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    }else{
        layout.minimumLineSpacing = 15;
        layout.sectionInset = UIEdgeInsetsMake(0, 33, 0, 33);
    }
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, collectionViewWidth, collectionViewHeight) collectionViewLayout:layout];
    [self addSubview:collectionView];
    _collectionView = collectionView;
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.alwaysBounceHorizontal = YES;
    collectionView.clipsToBounds = NO;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerClass:[HVWBorderShownCardCell class] forCellWithReuseIdentifier:CellIdentifier];
    
    CGFloat pageScrollWidth = itemWidth;
    if (type == 1) {
        pageScrollWidth = itemWidth + 7;
    }else{
        pageScrollWidth = itemWidth + 15;
    }
    _panScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((collectionView.frame.size.width - pageScrollWidth)/2, 0, pageScrollWidth, collectionViewHeight)];
    
    [self addSubview:_panScrollView];
    _panScrollView.hidden = YES;
    _panScrollView.showsHorizontalScrollIndicator = NO;
    _panScrollView.alwaysBounceHorizontal = YES;
    _panScrollView.pagingEnabled = YES;
    _panScrollView.delegate = self;
    
    [_collectionView addGestureRecognizer:_panScrollView.panGestureRecognizer];
    _collectionView.panGestureRecognizer.enabled = NO;
}

- (void)setImages:(NSArray *)images {
    _images = [images copy];
    [self updateView];
}

- (void)updateView {
    [_collectionView reloadData];
    [self addTimer];
}

- (void)addTimer {
    if (self.timer.isValid) {
        [self.timer invalidate];
    }
    
    self.timer = [NSTimer timerWithTimeInterval:5.0f target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)autoScroll {
    if (_images.count <= 1) {
        return;
    }
    
    // 滚到最后一页的时候，回到第一页
    if (_panScrollView.contentOffset.x >= _panScrollView.frame.size.width * (_images.count - 1)) {
        [_panScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else {
        [_panScrollView setContentOffset:CGPointMake(_panScrollView.contentOffset.x + _panScrollView.frame.size.width, 0) animated:YES];
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    _panScrollView.contentSize = CGSizeMake(_panScrollView.frame.size.width * _images.count, 0);
    return _images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HVWBorderShownCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if (indexPath.item < _images.count) {
        UIImage *image = _images[indexPath.item];
        cell.image = image;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (![[HomeList sharedInstance].hadRealName isEqualToString:@"4"]) {
        
        
        return [self goRealNameWithFlag:[HomeList sharedInstance].hadRealName];
    }
    
    if (self.urlArr.count <= 0) {
        
        return;
    }
    
    NSString *str = self.urlArr[indexPath.row];
    if (str.length <= 0) {
        
        return;
    }
    
    ActivityDetailController *VC = [[ActivityDetailController alloc]init];
    VC.imagepath = str;
    [[AppDelegate sharedAppDelegate] pushViewController:VC];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _panScrollView) {
        _collectionView.contentOffset = _panScrollView.contentOffset;
    }
}

#pragma mark - 前往实名认证
- (void)goRealNameWithFlag: (NSString *)flag{
    
    NSString *mes = @"您尚未进行实名认证";
    NSString *title = @"前往认证";
    if ([flag isEqualToString:@"2"]) {
        
        mes = @"您已提交实名认证，请耐心等待审核";
        title = @"前往修改";
    }
    
    if ([flag isEqualToString:@"3"]) {
        
        mes = @"实名认证未通过，请重新实名认证";
        title = @"重新认证";
    }
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:mes preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        ScanIDCardFontViewController *VC = [[ScanIDCardFontViewController alloc] init];
        [VC setHidesBottomBarWhenPushed:YES];
        [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
        
    }]];
    [[self getCurrentViewController] presentViewController:alert animated:YES completion:^{
        
    }];
}

@end
